/* 
 * Modbus app for RTU.
 * Copyright (c) 2012  apleilx
 * All rights reserved.
 * no limite
 */

/* ----------------------- Platform includes --------------------------------*/
#define _MBAPP_MODULE_
#include "mbapp.h"
#include "osObjects.h"                                          // RTOS object definitions

uint16_t DataRecevie[32];

uint8_t mbTestErrCnt = 0;
uint8_t mbTestOkCnt = 0;

void mbTestCallBack(uint8_t CmdSite, uint8_t ErrSt);

/*****************************************************************************//*!
* @brief   	Slave device handle.
*               
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/    
void ModbusTask (void  *p_arg)
{
    OS_ERR      p_err;
    CPU_TS      p_ts;
    
    OS_FLAGS    EventFlag;
        
    OSTaskSemSet(&ModbusTaskTCB, 0, &p_err);
    
    mbxInit(MB_RTU_SLAVE, 9600, MB_PAR_EVEN);
    mbxObj.SlaveId=2;
    
    mbyInit(MB_RTU_MASTER, 9600, MB_PAR_EVEN);
    mbyObj.SlaveId=3;
    
    mbzInit(MB_RTU_SLAVE, 9600, MB_PAR_EVEN);
    mbzObj.SlaveId=4;
    
    RegHolding[0] = 00;
    RegHolding[1] = 10;
    RegHolding[2] = 20;
    RegHolding[3] = 30;
    RegHolding[4] = 40;
    
    //mbStaticCmdSet(&mbyObj, 0, 4, FUN_CODE_READ_REG, DataRecevie, 0, 8, NULL);
    mbDynaCmdReq(&mbyObj, 4, FUN_CODE_READ_REG, DataRecevie, 0, 1, &mbTestCallBack);
    mbDynaCmdReq(&mbyObj, 4, FUN_CODE_READ_REG, DataRecevie, 0, 2, &mbTestCallBack);
    mbDynaCmdReq(&mbyObj, 4, FUN_CODE_READ_REG, DataRecevie, 0, 3, &mbTestCallBack);
    mbDynaCmdReq(&mbyObj, 4, FUN_CODE_READ_REG, DataRecevie, 0, 4, &mbTestCallBack);
    mbDynaCmdReq(&mbyObj, 4, FUN_CODE_READ_REG, DataRecevie, 0, 5, &mbTestCallBack);
    mbDynaCmdReq(&mbyObj, 4, FUN_CODE_READ_REG, DataRecevie, 0, 6, &mbTestCallBack);
    mbDynaCmdReq(&mbyObj, 4, FUN_CODE_READ_REG, DataRecevie, 0, 7, &mbTestCallBack);
    mbDynaCmdReq(&mbyObj, 4, FUN_CODE_READ_REG, DataRecevie, 0, 8, &mbTestCallBack);
    mbDynaCmdReq(&mbyObj, 4, FUN_CODE_READ_REG, DataRecevie, 0, 9, &mbTestCallBack);
    mbDynaCmdReq(&mbyObj,12, FUN_CODE_READ_REG, DataRecevie, 0, 10, &mbTestCallBack); 
    mbDynaCmdReq(&mbyObj, 4, FUN_CODE_READ_REG, DataRecevie, 0, 11, &mbTestCallBack);
    mbDynaCmdReq(&mbyObj, 4, FUN_CODE_READ_REG, DataRecevie, 0, 12, &mbTestCallBack);
    mbDynaCmdReq(&mbyObj, 4, FUN_CODE_READ_REG, DataRecevie, 0, 13, &mbTestCallBack);
    mbDynaCmdReq(&mbyObj, 4, FUN_CODE_READ_REG, DataRecevie, 0, 14, &mbTestCallBack);
    mbDynaCmdReq(&mbyObj, 4, FUN_CODE_READ_REG, DataRecevie, 0, 15, &mbTestCallBack);
    
    for(;;){
        
        EventFlag = OSFlagPend (&FlagModbusEvent,MBAPP_EVENT_MSK,1000,OS_OPT_PEND_FLAG_SET_ANY,&p_ts, &p_err);
        
        if(mbxObj.RunSt.bits.MasterMode){

        } else {
            if(EventFlag & (MBAPP_COM1_EVENT_RTUEND | MBAPP_COM1_EVENT_TIMEOUT)){
                OSFlagPost (&FlagModbusEvent, MBAPP_COM1_EVENT_RTUEND|MBAPP_COM1_EVENT_TIMEOUT, OS_OPT_POST_FLAG_CLR, &p_err);
                
                if(EventFlag & MBAPP_COM1_EVENT_TIMEOUT) 
                    mbxObj.RcvSt = MB_RX_TIMEOUT;
                else
                    printf(" COM1 modbus event: RTU received !\n");
            }
            mbSlavePoll(&mbxObj);
            
            if(EventFlag & (MBAPP_COM2_EVENT_RTUEND | MBAPP_COM2_EVENT_TIMEOUT)){
                OSFlagPost (&FlagModbusEvent, MBAPP_COM2_EVENT_RTUEND|MBAPP_COM2_EVENT_TIMEOUT, OS_OPT_POST_FLAG_CLR, &p_err);
                
                if(EventFlag & MBAPP_COM2_EVENT_TIMEOUT) 
                    mbyObj.RcvSt = MB_RX_TIMEOUT;
                else
                    printf(" COM2 modbus event: RTU received !\n");
            }
            mbMasterPoll(&mbyObj);
            
            if(EventFlag & (MBAPP_COM3_EVENT_RTUEND | MBAPP_COM3_EVENT_TIMEOUT)){
                OSFlagPost (&FlagModbusEvent, MBAPP_COM3_EVENT_RTUEND|MBAPP_COM3_EVENT_TIMEOUT, OS_OPT_POST_FLAG_CLR, &p_err);
                
                if(EventFlag & MBAPP_COM3_EVENT_TIMEOUT) 
                    mbzObj.RcvSt = MB_RX_TIMEOUT;
                else
                    printf(" COM3 modbus event: RTU received !\n");
            }
            mbSlavePoll(&mbzObj);
        }
    }
}


void mbTestCallBack(uint8_t CmdSite, uint8_t ErrSt){
    if(ErrSt)
        mbTestErrCnt++; 
    else
        mbTestOkCnt++;
}

